<?php
    $nombre = "Ajuste_Inventario_{$tienda}_".date("d_m_Y");
    $titulo = "Ajuste Inventario de la Tienda {$tienda}";

    $tit = array(
        array('#','STR','C'),
        array('COD. MATERIAL','STR','C'),
        array('DESCRIPCION','STR','C'),
        array('UNIDAD','STR','C'),
        array('CANT. SAP','STR','C'),
        array('CANT. SIGESP','STR','C'),
        array('DIFERENCIA','STR','C'),
        array('TIPO MOV.','STR','C')
    );
    /*pr($tit);
    pr($data);
    die();*/

    $this->PhpExcel->Excel();
    $ext = 'xlsx';
    $arrStyleTitle = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,
                                'startcolor' => array('argb' => '81DAF5')),
                            'font' => array('bold' => true, 'color' => array('rgb' => PHPExcel_Style_Color::COLOR_WHITE)),
                            'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER));

    $arrStyleTotales = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,
                                'startcolor' => array('argb' => '81DAF5')),
                            'font' => array('bold' => true, 'color' => array('rgb' => PHPExcel_Style_Color::COLOR_WHITE)));

    $arrStyleTitleuno = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('argb' => 'EFFBFB')));
    $arrborderTitleuno = array('style' => PHPExcel_Style_Border::BORDER_DASHDOT,'color' => array('rgb' => '000000'));

    $this->PhpExcel->titulo_reporte("AJUSTE INVENTARIO", $titulo);
    $this->PhpExcel->titulo_hoja("TIENDA $tienda");	

    $this->PhpExcel->hoja->getDefaultStyle()->applyFromArray(array('font' => array('name' => 'arial','size' => 10)));
    $this->PhpExcel->hoja->getStyle('A1')->applyFromArray(array('font' => array('bold' => true, 'size' => 12, 'color' => array('argb' => '8B0000'))));
    $this->PhpExcel->hoja->getStyle('A2')->applyFromArray(array('font' => array('bold' => true, 'size' => 11)));
    $this->PhpExcel->hoja->getStyle('A3')->applyFromArray(array('font' => array('bold' => false,'size' => 10)));
    $this->PhpExcel->hoja->getStyle('A4')->applyFromArray(array('font' => array('bold' => false,'size' => 10)));

    $this->PhpExcel->configura_celda('A',5); //#
    $this->PhpExcel->configura_celda('B',20); //COD. MATERIAL
    $this->PhpExcel->configura_celda('C',55); //DESCRIPCION
    $this->PhpExcel->configura_celda('D',15); //UNIDAD
    $this->PhpExcel->configura_celda('E',15); //CANT. SAP
    $this->PhpExcel->configura_celda('F',15); //CANT. SIGESP
    $this->PhpExcel->configura_celda('G',15); //DIFERENCIA
    $this->PhpExcel->configura_celda('H',20); //TIPO MOV.
    
    $this->PhpExcel->hoja->setAutoFilter('A6:H6'); // AUTO FILTRO

    $index_title = 6;
    $index_detail = 0;
    $j = 0;

    foreach($tit as $columnTitle) {
        $this->PhpExcel->valor_celda($j, $index_title, $columnTitle);
        $oColumn = $this->PhpExcel->obtener_columna($j, $index_title);
        $oRow = $this->PhpExcel->obtener_fila($j, $index_title);
        $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->applyFromArray($arrStyleTitle);
        $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->getBorders()->getAllBorders()->applyFromArray( $arrborderTitleuno);
        $j++;
    }

    $fila = 7;
    $i = 1;
    foreach($data as $indice => $valor) {
        $valores = array();
        if($tipo == 5)
            $valor[0]['tipo'] = 'No Existe';
        else
            $valor[0]['tipo'] = ($valor[0]['tipo'] == 1) ? 'Entrada' : 'Salida';
        $valores[] = array($i, 'STR', 'C');
        $valores[] = array($valor[0]['codart'],'STR', 'C');
        $valores[] = array($valor[0]['descripcion'],'STR', 'L');
        $valores[] = array($valor[0]['unidad'],'STR', 'C');
        $valores[] = array($valor[0]['cantidad_sap'],'N', 'R');
        $valores[] = array($valor[0]['cantidad_sigesp'],'N', 'R');
        $valores[] = array($valor[0]['diferencia'],'N', 'R');
        $valores[] = array($valor[0]['tipo'],'STR', 'C');

        $columna = 0;
        foreach($valores as $valor2) {
            $this->PhpExcel->valor_celda($columna, $fila,  $valor2);
            $oColumn = $this->PhpExcel->obtener_columna($columna, $fila);
            $oRow = $this->PhpExcel->obtener_fila($columna, $fila);

            if($i%2 == 0)
                $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->applyFromArray($arrStyleTitleuno);

                $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->getBorders()->getAllBorders()->applyFromArray($arrborderTitleuno);
                $columna++;
        }
        $fila++;
        $i++;
    }
    $this->PhpExcel->mostrar($nombre,$ext);